const { resolveApp } = require('./resolve');
const package = require(`${resolveApp('')}/package.json`);
const syfPackage = require('../package.json');
const deps = package.dependencies;
const npmAllow = [
  //react拖拽库
  'react-dnd',
  'react-dnd-html5-backend',
  'cross-env',
  //d3画布
  'd3-selection',
  'd3-zoom',
  'element-resize-detector',
  //国际化
  'i18next',
  'react-i18next',
  'i18next-browser-languagedetector',
  //使用immer
  // 'immutability-helper',
  'lodash-es',
  'mitt',
  'moment',
  'querystring',
  'immer',
  'postcss',
  'postcss-loader',
  ...Object.keys(syfPackage.dependencies),
];
const npmRefuse = [
  '@seeyon/ui/icon',
  '@ant-design/icons',
  'antd',
  'pinyin-pro',
  'nodejieba',
  'pinyin',
];
const npmCheck = async () => {
  let npmWarn = [],
    npmNot = [];
  const devDeps = package.devDependencies || {};
  Object.keys({ ...devDeps, ...deps }).forEach((dep) => {
    if (npmRefuse.indexOf(dep) > -1 || dep.indexOf('antd/') === 0) {
      npmNot.push(dep);
    }
    if (
      dep &&
      (dep.indexOf('@seeyon') === 0 || dep.indexOf('@types') === 0 || npmAllow.indexOf(dep) > -1)
    ) {
      return true;
    } else {
      npmWarn.push(dep);
    }
  });
  if (npmNot.length > 0 && package.name !== 'cdp-data' && package.name !== 'cip-manager') {
    console.log('\x1b[31m❌ERROR %s\x1b[0m', `不允许的npm引用：${npmNot}`);
    // 临时放开cdp-data
    process.exit(1);
  }
  if (npmWarn.length > 0) {
    console.log('\x1b[33m WARNING %s\x1b[0m', `即将不允许的npm引用：${npmWarn}`);
    // console.log('\x1b[31m❌ERROR %s\x1b[0m', `不允许的npm引用：${npmWarn}`);
    // process.exit(1);
  }
};

module.exports = {
  npmCheck,
};
